package com.dgshanger.sanhxiaofeisc.adapter

import com.amap.api.maps2d.AMapUtils
import com.amap.api.maps2d.model.LatLng
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.dgshanger.sanhxiaofeisc.R
import com.dgshanger.sanhxiaofeisc.common.Constants
import com.dgshanger.sanhxiaofeisc.entity.HomeEntity
import com.dgshanger.sanhxiaofeisc.ui.home.utils.ImageLoader
import java.math.BigDecimal

/**
 * Created by JacobHHH on 2018/4/17.
 */
class HomeAdapter(data: List<HomeEntity.RecommendListBean>) : BaseMultiItemQuickAdapter<HomeEntity.RecommendListBean,BaseViewHolder>(data) {

    init {
        addItemType(1,R.layout.item_home_shop)//商家
        addItemType(2,R.layout.item_home_image)//图片
        addItemType(3,R.layout.item_home_video)//视频
    }

    override fun convert(helper: BaseViewHolder, item:HomeEntity.RecommendListBean) {
        when(helper.itemViewType){
            1->{//商家
                ImageLoader.loadToUrl(mContext,helper.getView(R.id.iv_shop),Constants.getBaseUrl()+item.join_shop.menu_img)
                helper.setText(R.id.tv_name,item.join_shop.shop_name)
                helper.setText(R.id.tv_classify,item.join_shop.category_name)
                helper.setText(R.id.tv_intro,item.join_shop.introduce)
                helper.setText(R.id.tv_grade,mContext.resources.getString(R.string.tv_grade)+item.join_shop.comment_score)
                val location = Constants.getLocation()
                val myLatLng = LatLng(location[0].toDouble(), location[1].toDouble())
                val shopLatLng = LatLng(item.join_shop.latitude.toDouble(), item.join_shop.longitude.toDouble())
                val distanceMeters = AMapUtils.calculateLineDistance(myLatLng, shopLatLng).toDouble()
                val scale = BigDecimal(distanceMeters / 1000).setScale(2, BigDecimal.ROUND_DOWN)
                helper.setText(R.id.tv_distance,scale.toString()+"KM")
             }
            2->{//图片
                ImageLoader.loadToUrl(mContext,helper.getView(R.id.iv_photo),Constants.getBaseUrl()+item.join_activity_img.menu_img)
            }
            3->{//视频
                ImageLoader.loadToUrl(mContext,helper.getView(R.id.iv_video),Constants.getBaseUrl()+item.join_activity_video.video_thumb)
            }
        }
    }




}